package com.tanhua.dubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.UserInfoAPI;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.vo.ContactVo;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.RecommendUserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserInfoAPIImpl implements UserInfoAPI {

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public void save(UserInfo userInfo) {
        if (userInfoMapper.selectById(userInfo.getId())==null){
            userInfoMapper.insert(userInfo);
        }

    }

    @Override
    public void update(UserInfo userInfo) {
        UserInfo dbUserInfo = userInfoMapper.selectById(userInfo.getId());
        if (StringUtils.isNotBlank(userInfo.getAvatar())){
            dbUserInfo.setAvatar(userInfo.getAvatar());
        }
        if (StringUtils.isNotBlank(userInfo.getNickname())){
            dbUserInfo.setNickname(userInfo.getNickname());
        }
        if (StringUtils.isNotBlank(userInfo.getBirthday())){
            dbUserInfo.setBirthday(userInfo.getBirthday());
        }
        if (StringUtils.isNotBlank(userInfo.getGender())){
            dbUserInfo.setGender(userInfo.getGender());
        }
        userInfoMapper.updateById(dbUserInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public Page<UserInfo> findTuiJianLiebiao(List<Long> idList, RecommendUserVo vo) {
        //1 创建QueryWraper对象
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        //2 添加条件
        wrapper.in("id",idList);
        if (vo.getAge()!=null){
            wrapper.lt("age",vo.getAge());
        }
        if (StringUtils.isNotBlank(vo.getGender())){
            wrapper.eq("gender",vo.getGender());
        }
        if (StringUtils.isNotBlank(vo.getEducation())){
            wrapper.eq("education",vo.getEducation());
        }
        if (StringUtils.isNotBlank(vo.getCity())){
            wrapper.eq("city",vo.getCity());
        }
        //3 分页
        Page<UserInfo> page = new Page<>(vo.getPage(), vo.getPagesize());
        //4 查询
        userInfoMapper.selectPage(page,wrapper);
        //5 返回
        return page;
    }

    @Override
    public PageResult findByCondition(Integer page, Integer pagesize, String keyword, List<Long> firendIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",firendIdList);
        if (StringUtils.isNotBlank(keyword)){
            queryWrapper.like("nikename",keyword);
        }
        Page myPage = new Page<>(page, pagesize);
        userInfoMapper.selectPage(myPage,queryWrapper);
        //封装一下结果
        PageResult pr = new PageResult();
        pr.setPage(page);
        pr.setPagesize(pagesize);
        pr.setCounts((int)myPage.getTotal());
        Long pages = myPage.getTotal()%pagesize==0?myPage.getTotal()/pagesize:(myPage.getTotal()/pagesize+1);
        pr.setPages(pages.intValue());

        //转集合
        List<UserInfo> records = myPage.getRecords();
        List<ContactVo> contactVoList = new ArrayList<>();

        for (UserInfo ui:records){
            ContactVo contactVo = new ContactVo();
            BeanUtils.copyProperties(ui,contactVo);
            contactVo.setUserId(ui.getId().toString());
            contactVoList.add(contactVo);
        }

        pr.setItems(contactVoList);

        return pr;
    }

    @Override
    public List<UserInfo> findNearUserByGenderAndIdList(String gender, List<Long> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",userIdList);
        if (StringUtils.isNotBlank(gender)){
            queryWrapper.eq("gender",gender);
        }
        return userInfoMapper.selectList(queryWrapper);
    }
}
